<?php
namespace cf;

class MailTemplate
{
	function __construct($id) {
		$tpl = query2array('SELECT subject,body FROM cf_mail_templates WHERE id=:id', array('id'=>$id));
		$this->subject = $tpl['subject'];
		$this->body = $tpl['body'];
	}
	
	public function parseSubject($params) {
		return self::parse($this->subject,$params);
	}
	
	public function parseBody($params) {
		return self::parse($this->body,$params);
	}
	
	static private function parse($tpl,$params) {
		$smarty = new \Smarty;
		foreach ($params as $nm =>$val) {
			$smarty->assign($nm,$val);
		}
		return $smarty->fetch('string:'.$tpl,$params);
	}
	
	private $subject = '';     
	private $body = '';
};
?>